package me.firstapp.api.shop;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.ProductComment;
import me.firstapp.service.shop.ProductCommentService;

@Controller
@RequestMapping(value = "/productComment")
@Api(value = "/productComment", description = "积分相关接口")
public class ProductCommentApi extends BaseApi {

	@Autowired
	private ProductCommentService productCommentService;

	@RequestMapping(value = "/findProductCommentList.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询商品评论列表", notes = "查询商品评论列表")
	public void findProductCommentList(Long productId, HttpServletRequest request, HttpServletResponse response) {
		ListObject<ProductComment> result = new ListObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<ProductComment> list = productCommentService.findProductCommentList(productId, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(list);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/addProductComment.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增商品评论", notes = "新增商品评论")
	public void addProductComment(@RequestBody Map<String, Object> body, String orderNo, Long productId,
			Long fromMemberId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<ProductComment> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String content = (String) body.get("content");
			ProductComment comment = productCommentService.addProductComment(orderNo, productId, content, fromMemberId,
					apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(comment);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
